/*
 * Copyright 2010 Mttang.com All right reserved. This software is the
 * confidential and proprietary information of Mttang.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Mttang.com.
 */
package com.mttang.sys.biz.action;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.mttang.framework.core.action.AbstractAction;
import com.mttang.framework.core.exception.MtTangServiceRuntimeException;
import com.mttang.framework.core.pojos.ServiceRequest;
import com.mttang.framework.core.pojos.ServiceResponse;
import com.mttang.sys.biz.service.UserService;
import com.mttang.sys.dto.UserDTO;
import com.mttang.util.SysBeanConstant;

/**
 * 类LoginAction.java的实现描述：TODO 类实现描述
 * 
 * @author acc 2010-6-4 下午04:19:04
 */
public class LoginAction extends AbstractAction {

	private static final String STATUS_TRUE = "true";

	private UserService service ;

	public void setService(UserService service) {
		this.service = service;
	}

	private static final String STATUS_FALSE = "false";

	private Log logger = LogFactory.getLog(getClass());

	public ServiceResponse perform(ServiceRequest request)
			throws MtTangServiceRuntimeException {
		return null;
	}

	public Map<Object, Object> login(Map<String, Object> map2) {
		UserDTO dto = new UserDTO();
		dto.setEmpno(String.valueOf(map2.get("empno")));
		dto.setPassWord(String.valueOf(map2.get("passWord")));
		Map<Object, Object> logMap = (Map<Object, Object>) map2.get("LOGMAP");
		UserDTO userDto = service.login(dto, logMap);
		Map<Object, Object> map = new HashMap<Object, Object>();
		if (userDto != null) {
			map.put("userid", userDto.getId());
			map.put("username", userDto.getName());
			map.put("passWord", userDto.getPassWord());
			map.put("phone", userDto.getPhone());
			map.put("email", userDto.getEmail());
			map.put("createtime", userDto.getCreateTime());
			map.put("state", userDto.getState());
			map.put("depId", userDto.getDepId());
			map.put("roleId", userDto.getRoleId());
			map.put("empno", userDto.getEmpno());
			map.put("depName", userDto.getDepName());
			map.put("roleName", userDto.getRoleName());
		}
		return map;
	}
	
	public Map<Object,Object> pass(Map<String,Object> map2){
		String empno = String.valueOf(map2.get("empno"));
		String pass = String.valueOf(map2.get("pass"));
		UserDTO dto = service.findByEmpno(empno);
		Map<Object, Object> map = new HashMap<Object, Object>();
		if (dto != null && dto.getId() != null) {

			// AccountFacade emailService =
			// (AccountFacade)context.getBean("accountFacade");
			/* 解析请求参数，组装DTO对象 */
			String subject = (String) map2.get("subject");
			String content = (String) map2.get("content");

			/* 调用业务层对象，执行业务逻辑 */
			String errorkey = null;
			String strStatus = STATUS_FALSE;
			// int result = emailService.sendEmail("", dto.getEmail(),
			// subject, content);

			/* 返回WEB层结果 */
			int result = -1;
			switch (result) {
			case -1:
				strStatus = STATUS_FALSE;
				errorkey = "OK";
				break;
			case -2:
				strStatus = STATUS_FALSE;
				errorkey = "error";
				break;
			case 0:
				strStatus = STATUS_TRUE;
				break;
			default:
				strStatus = STATUS_FALSE;
			}

			Map<Object, Object> model = new HashMap<Object, Object>();
			map.put("status", strStatus);
			if (errorkey != null) {
				map.put("errorkey", errorkey);
				
				return map;
			}
			UserService userService = (UserService) context
					.getBean(SysBeanConstant.SERVICE_USER);
			UserDTO user = new UserDTO();
			user.setId(dto.getId());
			user.setPassWord(pass);
			Map<Object, Object> logMap = (Map<Object, Object>)map2.get("LOGMAP");
			userService.update(user, logMap);
			map.put("email", dto.getEmail());
		} else {
			map.put("message", "1");// 请输入真确的工号！！
		
			return map;
		}
		return map;
	}
	

}
